package cn.udesk.web.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.date.DateUtil;
import cn.udesk.common.toolkit.Constant;
import cn.udesk.core.dto.BootTablePage;
import cn.udesk.core.mapper.KTransRecordDao;
import cn.udesk.core.model.KTransRecord;

@Service
public class TransRecordService {

  @Autowired
  private KTransRecordDao kTransRecordDao;

  /**
   * @Title getList
   * @Description 获取列表
   * @param start 其实行数
   * @param size 结束行数
   * @param uId 用户ID
   * @param transId 转换ID
   * @return
   * @return BootTablePage
   */
  public BootTablePage getList(Integer start, Integer size, Integer uId, Integer transId) {
    KTransRecord template = new KTransRecord();
    template.setAddUser(uId);
    if (transId != null) {
      template.setRecordTrans(transId);
    }
    List<KTransRecord> kTransRecordList = kTransRecordDao.template(template, start, size);
    long totalCount = kTransRecordDao.templateCount(template);
    BootTablePage bootTablePage = new BootTablePage();
    bootTablePage.setRows(kTransRecordList);
    bootTablePage.setTotal(totalCount);
    return bootTablePage;
  }

  /**
   * @Title getLogContent
   * @Description 转换日志内容
   * @param recordId 转换记录ID
   * @return
   * @throws IOException
   * @return String
   */
  public String getLogContent(Integer recordId) throws IOException {
    KTransRecord kTransRecord = kTransRecordDao.unique(recordId);
    String logFilePath = kTransRecord.getLogFilePath();
    File file = new File(logFilePath);
    if (!file.exists()) {
      return "未找到日志文件，可能已被删除";
    }
    return FileUtils.readFileToString(file, Constant.DEFAULT_ENCODING);
  }


  public Map<String, Object> getTransLine() {
    SQLManager sqlManager = kTransRecordDao.getSQLManager();
    String sql =
        "SELECT DATE_FORMAT(start_time,'%Y-%d-%m') AS stat_at,sum(CASE WHEN record_status=1 THEN 1 ELSE 0 END) AS successCount,sum(CASE WHEN record_status=2 THEN 1 ELSE 0 END) AS failCount FROM k_trans_record "
            + "WHERE start_time BETWEEN ? AND ? GROUP BY DATE_FORMAT(start_time,'%Y-%d-%m') order by stat_at";
    @SuppressWarnings("rawtypes")
    List<Map> list = sqlManager.execute(new SQLReady(sql,
        DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -30)), DateUtil.endOfDay(new Date())),
        Map.class);
    HashMap<String, Object> resultMap = new HashMap<String, Object>();
    List<Integer> successData = new ArrayList<Integer>();
    List<Integer> failData = new ArrayList<Integer>();
    list.forEach(i -> {
      successData.add(Integer.parseInt(i.get("successCount").toString()));
      failData.add(Integer.parseInt(i.get("failCount").toString()));
    });
    resultMap.put("successName", "成功数");
    resultMap.put("successData", successData);
    resultMap.put("failName", "失败数");
    resultMap.put("failData", failData);
    return resultMap;
  }
}
